//
//  OEPresuppShopViewController.m
//  OneEightyNine
//
//  Created by Mac on 2019/3/5.
//  Copyright © 2019 stoneobs. All rights reserved.
//

#import "OEPresuppShopViewController.h"
#import "OEPresupShopTableViewCell.h"
#import "OEPresuppSuccessViewController.h"
@interface OEPresuppShopViewController ()
@property(nonatomic, strong) NSMutableArray                     *dataSouce;/**< <##> */
@end

@implementation OEPresuppShopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    __weak typeof(self) weakSelf =  self;
    self.title = OESTRING(@"CAFE NEARBY");
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 150)];
    imageView.image = [TMRandImageTool bundleImageWithImageName:TMRandImageTool.randOneGoodBanner];
    self.tableView.tableHeaderView = imageView;
    self.tableView.backgroundColor = OE_GrayColor;
    self.tableView.st_noreslutView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 170, UIScreenWidth, 400) title:OESTRING(@"NO More Data") buttonTitle:@"" buttonHandle:^(NSString *titleString) {
        
    }];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf sendNetListRequest];
    }];          
    [self.tableView.mj_header beginRefreshing];
    

    // Do any additional setup after loading the view.
}

#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return OEPresupShopTableViewCell.cellHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    OEPresupShopTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[OEPresupShopTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    OEShopEasyModel * model = self.dataSouce[indexPath.row];
    cell.model = model;
    __weak typeof(self) weakSelf =  self;
    [cell.appoinmentButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedMakeOrderButtonWithModel:model];
    }];
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    OEShopEasyModel * model = self.dataSouce[indexPath.row];
    [TMUtils gotoShopHeaderWithModel:model];
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
- (void)onSelectedMakeOrderButtonWithModel:(OEShopEasyModel*)model{
    BOOL isOpenFaceOrFinger = [[NSUserDefaults standardUserDefaults] boolForKey:OE_FACE_OR_FINGER_PAY_OPEN_KEY];
    if (isOpenFaceOrFinger) {
        [TMUtils showFaceOrFingerPayType:@"2" handle:^{
             [self sendMakeOrderRequestWithModel:model];
        } inputPwdHandle:^{
            OEPwdInPutViewController * vc = OEPwdInPutViewController.new;
            vc.pwd = STUserManger.defult.loginedUser.pay_password;
            [vc setOnInputTruePwd:^{
                [self sendMakeOrderRequestWithModel:model];
            }];
            [self presentViewController:vc animated:NO completion:nil];
            
        } errorHandle:^{
            
        }];
    }else{
        OEPwdInPutViewController * vc = OEPwdInPutViewController.new;
        vc.pwd = STUserManger.defult.loginedUser.pay_password;
        [vc setOnInputTruePwd:^{
            [self sendMakeOrderRequestWithModel:model];
        }];
        [self presentViewController:vc animated:NO completion:nil];
    }

    return;
    
}
#pragma mark --NetWork Method
- (void)sendNetListRequest{
    [SVProgressHUD showWithStatus:@"Waiting"];
    NSMutableDictionary * paramDic = [NSMutableDictionary new];
    NSString * token = STUserManger.defult.loginedUser.token;
    if (token.length) {
        [paramDic setObject:token forKey:@"user_token"];
    }
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php?m=Front&c=User&a=supplierList"];
    DDLogInfo(@"url:%@",url);
    DDLogInfo(@"param:%@",paramDic);
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:paramDic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 //                                                 [STNetWrokManger shownNormalRespMsgWithResponse:responseObject];
                                                 NSDictionary * modelDic = [responseObject valueForKey:@"content"];
                                                 NSArray * array = modelDic[@"list"];
                                                 self.dataSouce = [OEShopEasyModel mj_objectArrayWithKeyValuesArray:array];
                                                 [self.tableView reloadData];
                                                 [self.tableView.mj_header endRefreshing];
                                                 DDLogInfo(@"%@请求成功:resp\n%@",url,responseObject);
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error,NSError *originError) {
                                                 [SVProgressHUD dismiss];
                                                 self.dataSouce = [OEShopEasyModel mj_objectArrayWithKeyValuesArray:@[]];
                                                 [self.tableView reloadData];
                                                 [self.tableView.mj_header endRefreshing];
//                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                                                            url,paramDic,error.desc,error.code);
                                             }];
}
- (void)sendMakeOrderRequestWithModel:(OEShopEasyModel*)model{
    [SVProgressHUD showWithStatus:@"Waiting"];
    NSMutableDictionary * paramDic = [NSMutableDictionary new];
    NSString * token = STUserManger.defult.loginedUser.token;
    if (token.length) {
        [paramDic setObject:token forKey:@"user_token"];
    }
    if (self.start_time.length) {
        [paramDic setObject:self.start_time forKey:@"start_time"];
    }
    if (self.flavor_id.length) {
        [paramDic setObject:self.flavor_id forKey:@"flavor_id"];
    }
    if (self.meal_id.length) {
        [paramDic setObject:self.meal_id forKey:@"meal_id"];
    }
    if (self.people_id.length) {
        [paramDic setObject:self.people_id forKey:@"people_id"];
    }
    if (self.end_time.length) {
        [paramDic setObject:self.end_time forKey:@"end_time"];
    }
    if (model.supplier_id.length) {
        [paramDic setObject:model.supplier_id forKey:@"supplier_id"];
    }
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php?m=Front&c=User&a=userReserve"];
    DDLogInfo(@"url:%@",url);
    DDLogInfo(@"param:%@",paramDic);
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:paramDic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 //                                                 [STNetWrokManger shownNormalRespMsgWithResponse:responseObject];
                                                 NSDictionary * modelDic = [responseObject valueForKey:@"content"];
                                                 [self.navigationController pushViewController:OEPresuppSuccessViewController.new animated:YES];
                                                 DDLogInfo(@"%@请求成功:resp\n%@",url,responseObject);
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error,NSError *originError) {
                                                 [SVProgressHUD dismiss];
                                                 //                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                                                            url,paramDic,error.desc,error.code);
                                             }];
}
@end
